package com.dingli.admin.service.impl;

import com.dingli.admin.service.IUserService;
import com.dingli.framework.entity.User;
import com.dingli.framework.mapper.UserMapper;
import com.dingli.framework.util.Response;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IUserServiceIpl implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Response<PageInfo<User>> list(String search, Integer pageNum, Integer pageSize) {
        if (pageNum == null) {
            pageNum = 1;
        }
        if (pageSize == null) {
            pageSize = 5;
        }
        PageHelper.startPage(pageNum, pageSize);
        PageInfo<User> info = new PageInfo<>(userMapper.list(search));
        return new Response<>(200, "success", info);
    }

    @Override
    public Response<Long> insert(User user) {
        int count = userMapper.insert(user);
        if (count > 0) {
            return new Response<>(200, "success", user.getUser_id());
        } else {
            return new Response<>(500, "failed");
        }
    }

    @Override
    public Response<User> selectById(Long userId) {
        return new Response<>(200, "success", userMapper.selectById(userId));
    }

    @Override
    public Response<Object> updateById(User user) {
        int count = userMapper.updateById(user);
        if (count > 0) {
            return new Response<>(200, "success");
        } else {
            return new Response<>(500, "failed");
        }
    }

    @Override
    public Response<Object> deleteById(Long userId) {
        int count = userMapper.deleteById(userId);
        if (count > 0) {
            return new Response<>(200, "success");
        } else {
            return new Response<>(500, "failed");
        }
    }

    @Override
    public Response<Object> deleteBatch(List<Long> idList) {
        int count = userMapper.deleteBatch(idList);
        if (count > 0) {
            return new Response<>(200, "success");
        } else {
            return new Response<>(500, "failed");
        }

    }
}
